Skip to content
This repository has been archived by the owner on Dec 22, 2023. It is now read-only.

Feature/divvy #14

Merged
merged 4 commits into from Jan 23, 2013
Merged

Feature/divvy #14

merged 4 commits into from Jan 23, 2013

Conversation

rbt
Copy link

@rbt rbt commented Jan 2, 2013

The user sets tick_division_type to one of the below module names (or their own module). This is loaded on the first call to divvy() and _real_divvy() is executed.

Divvy() was moved to Axis.pm from Data::Range.pm because it is a presentation side effect rather than something to do with the data itself. Producing a logarithmic display for the graph does not change the data which could be used in more than one location.

Exact is the old mechanism of dividing the space into equal tick segments.

exact

LinearRounded produces values which are (close to?) visually pleasing (nice whole figures) while keeping the number of ticks somewhere around what the user requested (default of 5).

linearrounded

LinearExpandGraph is an experimental mechanism taken from branch divvy_strategy. In my opinion LinearRounded produces better results but having multiple examples shows that the module loading functions correctly.

linearexpandgraph

Most importantly, users may create and use their own Divvy mechanism. Time based divvy logic will be somewhat different than the simple number set I have done; so I expect the number of Divvy modules to increase.

Implementing a Logarithmic divvy() from here is straight forward. The rest of the adjustments for logarithmic output isn't so obvious.

Rod Taylor added 3 commits January 2, 2013 00:09
The user sets tick_division_type to one of the below module names (or their own module). This is loaded on the first call to divvy() and _real_divvy() is executed.

Divvy() was moved to Axis.pm from Data::Range.pm because it is a presentation side effect rather than something to do with the data itself. Producing a logarithmic display for the graph does not change the data which could be used in more than one location.

LinearRounded produces values which are visually pleasing (nice whole figures) while keeping the number of ticks somewhere around what the user requested (default of 5).

Exact is the old mechanism of dividing the space into equal tick segments.

LinearExpandGraph is an experimental mechanism taken from branch divvy_strategy. In my opinion LinearRounded produces better results but having multiple examples shows that the module loading functions correctly.
@rbt
Copy link
Author

rbt commented Jan 2, 2013

Included is the script I used to generate those images:

#!/usr/bin/env perl

use 5.14.2;

use Chart::Clicker;
use Chart::Clicker::Renderer::CandleStick;
use Chart::Clicker::Renderer::StackedBar;
use Chart::Clicker::Decoration::Legend::Tabular;
use Number::Format;

my $CHART_WIDTH       = '600';
my $MAIN_CHART_HEIGHT = '300';
my $SUB_CHART_HEIGHT  = '100';

#my $AXIS_DIVVY_TYPE = 'Exact';
#my $AXIS_DIVVY_TYPE = 'LinearRounded';
my $AXIS_DIVVY_TYPE = 'LinearExpandGraph';

# Necessary for alignment because the subgraph and main
# graph may have different widths
my $MINIMUM_AXIS_WIDTH = 75;

my %GRAPH_DATA = (
    price  => [ 28.20,  29,  29,  31.8,  30,  29,  27.50 ],     # Sample price data
    volume => [ 100, 200, 250, 300, 200, 100, 350 ],    # Sample volume data
    day_of_week => [ 1 .. 7 ],
);

my $cc = Chart::Clicker->new( format => 'png', width => $CHART_WIDTH, height => $MAIN_CHART_HEIGHT );
$cc->title->text('Price and Volume Chart');

# Add the volume subgraph
my $cvolumebar = Chart::Clicker->new( width => $CHART_WIDTH, height => $SUB_CHART_HEIGHT );
$cvolumebar->legend->visible(0);
$cc->add_subgraph($cvolumebar);

my $pricectx  = $cc->get_context('default');
my $volumectx = $cvolumebar->get_context('default');

my $priceSeries = Chart::Clicker::Data::Series->new(
    keys   => $GRAPH_DATA{'day_of_week'},
    values => $GRAPH_DATA{'price'},
    name   => 'Price'
);
my $priceDataSet = Chart::Clicker::Data::DataSet->new( series => [$priceSeries] );
$priceDataSet->context('default');
$cc->add_to_datasets($priceDataSet);

my $volumeSeries = Chart::Clicker::Data::Series->new(
    keys   => $GRAPH_DATA{'day_of_week'},
    values => $GRAPH_DATA{'volume'},
    name   => 'Volume',

);
my $volumeDataSet = Chart::Clicker::Data::DataSet->new(
    series => [$volumeSeries],
    lower  => 0
);
$volumeDataSet->context('default');
$cvolumebar->add_to_datasets($volumeDataSet);

$cc->plot->grid->visible(1);
$cvolumebar->plot->grid->visible(1);


$pricectx->range_axis->tick_division_type($AXIS_DIVVY_TYPE);
$pricectx->range_axis->label(q{Price});
$pricectx->range_axis->show_ticks(1);
$pricectx->range_axis->ticks(3);
$pricectx->range_axis->fudge_amount(0.01);
$pricectx->range_axis->minimum_width($MINIMUM_AXIS_WIDTH);

$pricectx->domain_axis->tick_division_type($AXIS_DIVVY_TYPE);
$volumectx->domain_axis->tick_division_type($AXIS_DIVVY_TYPE);
$pricectx->domain_axis->label('Day of Month');

$volumectx->range_axis->tick_division_type($AXIS_DIVVY_TYPE);
$volumectx->range_axis->label(q{Volume});
$volumectx->range_axis->ticks(2);
$volumectx->range_axis->minimum_width($MINIMUM_AXIS_WIDTH);

# Ensure the volume axis begins from 0. The default is to begin
# from the smallest volume given, which is 100 in this example
$volumectx->range_axis->range->lower(0);

# Keep vertical tick grid but skip the labels. 
$volumectx->domain_axis->show_ticks(1);
$volumectx->domain_axis->format( sub { return q{} });

# Render price as a line
$pricectx->renderer( Chart::Clicker::Renderer::Line->new() );

# Render volume as a bar
$volumectx->renderer( Chart::Clicker::Renderer::StackedBar->new() );

$cc->write_output(lc($AXIS_DIVVY_TYPE) .'.png');

It was mistakenly cutting off the display of the bottom of the chart by adjusting to the lowest tick. The graph may be drawn below the lowest tick.
gphat added a commit that referenced this pull request Jan 23, 2013
@gphat gphat merged commit 36aabb2 into gphat:master Jan 23, 2013
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants